#!/usr/bin/python
# -*- coding: utf-8 -*-
import json, time, os
from urllib.request import urlopen
import sys
from urllib.parse import quote
import glob
dataName = "AirPM"

url = "http://web.juhe.cn:8080/environment/air/pm?"
appkey = "9a9f9ab876cf3e1e5595c46b2780a664"
dataDirectoryPath = "/home/xiaoyu/Documents/DataVisualizationByD3/D3/publicdata/data/"+dataName+"/"
today = time.strftime('%Y-%m-%d', time.localtime(time.time()))


cityListPath = "../ConvinceCity/citylist.txt"
cityList = ""


def getAirPM(city):
    # if has existed,we shouldnt caught it again.
    if os.path.exists(dataDirectoryPath + today + "/" + today + "-" + city + ".json") == True:
        return

    # 如果API接口换了，改这里
    str = url + "city=" + city + "&key=" + appkey
    str = quote(str, safe="/=?:&")  # quote is great thing!
    print(city+":"+str)
    response = urlopen(str).read().decode('utf-8')
    responseJson = json.loads(response)

    # check the resultcode,if resultcode equals 200 then create new file to save json
    if responseJson.get("resultcode") == "200":
        try:
            file = open(dataDirectoryPath + today + "/" + today + "-" + city + ".json", "w+")
            json.dump(responseJson, file, ensure_ascii=False)
        finally:
            file.close()
    else:
        print(city)






if __name__ == '__main__':
    if os.path.exists(dataDirectoryPath) ==False:
        os.mkdir(dataDirectoryPath)
    if os.path.exists(dataDirectoryPath + today) == False:
        os.mkdir(dataDirectoryPath + today)

    citylist = open('../ConvinceCity/citylist.txt')
    try:
        for city in citylist:
            getAirPM(city.replace("\n", ""))
    finally:
        citylist.close()

    files = glob.glob(dataDirectoryPath + today + "/" + "*.json")
    print(files)
    result = []
    for file in files:
        f = open(file, 'r')
        a = json.load(f)
        result.append(a['result'][0])

    file = open(dataDirectoryPath + today + "/"  + today+"-all" + ".json", "w+")
    json.dump(result, file, ensure_ascii=False)
